package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.UserComment;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.UserCommentMapper;
import cn.wolfcode.trip.base.service.IUserCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/11/14.
 */
@Service
public class UserCommentServiceImpl implements IUserCommentService {

    @Autowired
    private UserCommentMapper commentMapper;

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    /**
     * 保存用户的攻略评论
     *
     * @param userComment
     */
    @Override
    public void saveStrategyComment(UserComment userComment) {
        if (UserComment.TYPE_STRATEGY.equals (userComment.getThemeType ())) {
            strategyCommentMapper.addCommentNum (userComment.getUsercommentId ());//评论数+1
        }
        userComment.setCommentTime (new Date ());
        commentMapper.insert (userComment);
    }

    /**
     * 根据游记id查询它的所有评论
     *
     * @param id
     * @return
     */
    @Override
    public List<UserComment> listByTravelId(Long id) {
        return commentMapper.listByTravelId (id);
    }

    /**
     * 保存一条评论的回复
     *
     * @param userComment
     */
    @Override
    public void saveCommentReply(UserComment userComment) {
        Long usercommentId = userComment.getUsercommentId ();
        UserComment commentbei = commentMapper.selectByPrimaryKey (usercommentId);
        userComment.setBecommented (commentbei.getUser ());
        userComment.setCommentTime (new Date ());
        commentMapper.insert (userComment);
    }
}
